package com.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.dao.IRetPrdInDao;
import com.model.Ret_prd_in;
import com.model.Ret_prd_inId;
import com.rtnBean.SimpleRtnBeen;
import static com.def.GenericDef.*;

@Repository
public class RetPrdInHibernateDao extends
		GenericHibernateDao<Ret_prd_in, Ret_prd_inId> implements IRetPrdInDao {

	public SimpleRtnBeen getRetPrdInByWoAndBox(String woId, String boxId) {
		Ret_prd_in ret_prd_in = new Ret_prd_in();
		String hqlQuery = "FROM Ret_prd_in where wo_id_fk = '" + woId
				+ "' AND " + " id.mtrl_box_id = '" + boxId + "' ";
		ret_prd_in = (Ret_prd_in) this.comTblRead(hqlQuery);
		if (null == ret_prd_in) {
			return new SimpleRtnBeen(E_RET_PRD_IN + E_READ_NOT_FOUND, null,
					"内部订单[" + woId + "] 和来料箱[" + boxId + "]在来料信息中不存在！");
		}
		return new SimpleRtnBeen(ret_prd_in);
	}

	public String getCusIdOfMtrlBox(String woId, String boxId) {
		String cus_id = null;
		String queryString = " SELECT CUS_ID,WO_ID_FK FROM RET_PRD_IN WHERE WO_ID_FK='"
				+ woId + "' AND MTRL_BOX_ID='" + boxId + "' LIMIT 1";

		List<Object[]> objList = this.findBySQL(queryString);
		if (objList.size() > 0) {
			cus_id = objList.get(0)[0].toString();
		}

		return cus_id;
	}

	public String getNxSlotNoInMtrlBox(Ret_prd_in ret_prd_in) {
		String maxSlotNoS = "", nxSlotNoS = "";
		Integer maxSlotNo = 0;
		String sqlStr = "SELECT MTRL_SLOT_NO FROM RET_PRD_INDT T WHERE T.MTRL_BOX_ID_FK ='"
				+ ret_prd_in.getId().getMtrl_box_id()
				+ "' ORDER BY MTRL_SLOT_NO*1 DESC LIMIT 1";
		Object slotO = this.findBySQLOnly(sqlStr);
		if (null == slotO) {
			nxSlotNoS = "1";
		} else {
			maxSlotNoS = slotO.toString();
			maxSlotNo = Integer.valueOf(maxSlotNoS);
			nxSlotNoS = (++maxSlotNo).toString();
		}
		return nxSlotNoS;
	}

	/******** 自动填充断掉的子格位号 *********/
	public String getNxSlotNoInMtrlBoxLoop(Ret_prd_in ret_prd_in) {
		String nxSlotNoS = "1";
		Integer slotNo_A = 0;
		Integer slotNo_B = 0;
		String sqlStr = "SELECT MTRL_SLOT_NO FROM RET_PRD_INDT T WHERE T.MTRL_BOX_ID_FK ='"
				+ ret_prd_in.getId().getMtrl_box_id()
				+ "' ORDER BY MTRL_SLOT_NO*1 ASC";
		List<Object> slotO = this.findBySQL(sqlStr);
		if (slotO.isEmpty()) {
			nxSlotNoS = "1";
		} else {
			for (int i = 0; i<slotO.size(); i++) {
				slotNo_A = Integer.valueOf(slotO.get(i).toString());
				if(i == 0 && slotNo_A != 1){
					nxSlotNoS = "1";
					break;
				}
				if((i+1) < slotO.size()){
					slotNo_B = Integer.valueOf(slotO.get(i+1).toString());
					if((slotNo_B - slotNo_A) > 1){
						nxSlotNoS = slotNo_A + 1 + "";
						break;
					}
				}else{
					nxSlotNoS = slotNo_A + 1 + "";
				}
			}
		}
		return nxSlotNoS;
	}

	public Integer getWaitBoxSumByWo(String wo_id) {
		Integer sum_qty = 0;
		String sqlStr = "SELECT SUM(PRD_TL_QTY) FROM RET_PRD_IN WHERE WO_ID_FK ='"
				+ wo_id + "' AND PPBOX_STAT='WAIT'";
		Object sum_qtyO = this.findBySQLOnly(sqlStr);
		if (null != sum_qtyO) {
			sum_qty = Integer.valueOf(sum_qtyO.toString());
		}
		return sum_qty;
	}

}
